package controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import po.Article;
import service.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ArticleController {

    //日志文件类
    private static final Logger logger = LoggerFactory.getLogger(ArticleController.class);

    @Autowired
    private JudgeServiceImpl judgeServiceImpl;
    @Autowired
    private InsertServiceImpl insertServiceImpl;
    @Autowired
    private SearchServiceImpl searchServiceImpl;
    @Autowired
    private DeleteServiceImpl deleteServiceImpl;

    private String sessionName = "loginUser";

    //访问写博文页面
    @RequestMapping("/writingArticle")
    public String  accessWritingArticlePage() throws Exception{
        return "writingArticle";
    }

    //访问读博文页面
    @RequestMapping("/readingArticle")
    public String  accessReadingArticlePage() throws Exception{
        return "readingArticle";
    }

    //访问博文列表页面
    @RequestMapping("/articleList")
    public String  accessArticleListPage() throws Exception{
        return "articleList";
    }

    //获取一篇具体的博文请求
    @RequestMapping("/requestArticle")
    public @ResponseBody
    List<Article>  requestArticle(@RequestBody Map<String,Integer> param){
        List<Article> resultArticle = new ArrayList<>();

        try {
            Article theArticle = searchServiceImpl.getTheArticle(param.get("id"));
            resultArticle.add(theArticle);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultArticle;
    }

    //删除一篇具体的博文请求
    @RequestMapping("/deleteArticle")
    public @ResponseBody
    Map deleteArticle(@RequestBody Map<String,Integer> param){
        boolean deleteResult = false;
        Map<String,Object> resultMap = new HashMap();

        try {
            deleteResult = deleteServiceImpl.deleteArticle(param.get("id"));
            deleteResult = true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        resultMap.put("result",deleteResult);
        return resultMap;
    }


    //获取一篇具体的博文请求
    @RequestMapping("/requestAllArticles")
    public @ResponseBody
    List<Article>  requestAllArticles(){
        List<Article> resultArticle = new ArrayList<>();

        try {
            resultArticle = searchServiceImpl.getAllArticles();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultArticle;
    }


    //提交博文
    @RequestMapping("/submitArticle")
    public @ResponseBody
    Map submitArticle(@RequestBody Article article){

        boolean judgeResult = false;
        judgeResult = insertServiceImpl.insertArticle(article);

        Map<String,Object> resultMap = new HashMap();
        resultMap.put("result",judgeResult);

        //测试
        if (judgeResult){
            resultMap.put("url","../home");
        }

        return resultMap;

    }


    //返回最新的三篇博文于主页显示
    @RequestMapping("/getNewestArticles")
    public @ResponseBody
    List<Article> getNewestArticles(){

        List<Article> resultArticles = new ArrayList<>();

        try {
            resultArticles = searchServiceImpl.getThreeNewestArticles();
        } catch (Exception e) {
            e.printStackTrace();
        }


        return resultArticles;

    }
}
